package cn.fx.user.controller;

import cn.fx.basic.util.JsonResult;
import cn.fx.basic.util.PageList;
import cn.fx.user.domain.Logininfo;
import cn.fx.user.query.LogininfoQuery;
import cn.fx.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
//接口文档的注解
@Api(value = "用户的API",description="用户相关的CRUD功能")
public class LogininfoController {

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 接口: 查询所有
     * http://localhost:8080/logininfo --- url
     * 动词: GET
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping //上面的结合
    @ApiOperation(value = "查询所有用户信息" ) //打在方法上，对方法做描述
    public List<Logininfo> findAll() {

        return logininfoService.loadAll();
    }

    /**
     * 接口: 查询单个对象
     *  http://localhost:8080/logininfo/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    public Logininfo findOne(@PathVariable("id") Long id) {

        return logininfoService.loadById(id);
    }

    /**
     * 接口: 分页查询
     * post - 参数通过json对象传递
     *
     * @RequestBody: 从请求体中获取数据赋值给logininfoQuery
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口" )
    public PageList<Logininfo> queryPage(@RequestBody LogininfoQuery query) {
        try {
            return logininfoService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 接口: 删除
     * @param id
     * @return
     *   JsonResult: 链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public JsonResult remove(@PathVariable("id") Long id) {
        try {
            logininfoService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }

    }

    /**
     *  http://localhost:8080/logininfo
     * 接口: 添加和修改
     * @param logininfo
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult addOrUpdate(@RequestBody Logininfo logininfo){
        try {
            if(logininfo.getId() == null){
                //添加操作
                logininfoService.save(logininfo);
            } else {
                //修改操作
                logininfoService.update(logininfo);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            logininfoService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }

    }
}
